﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    /// <summary>
    /// 负责平巷断面及支护模块具体业务逻辑的处理
    /// </summary>
    public class TunnelSectionService
    {
        internal static TunnelSectionOut TunnelSectionResult(TunnelSectionIn sectionIn)
        {

            //传出对象
            TunnelSectionOut sectionOut = new();

            CalSection(sectionIn, sectionOut);

            string zhihu_type = sectionIn.SupportType;
            switch (sectionIn.USteel)
            {
                case "18UY":
                    sectionOut.ZJ_PieceWeight = 18.96;
                    break;
                case "25UY":
                    sectionOut.ZJ_PieceWeight = 24.76;
                    break;
                case "25U":
                    sectionOut.ZJ_PieceWeight = 24.95;
                    break;
                case "29U":
                    sectionOut.ZJ_PieceWeight = 29;
                    break;
                case "36U":
                    sectionOut.ZJ_PieceWeight = 35.87;
                    break;
                case "40U":
                    sectionOut.ZJ_PieceWeight = 40.05;
                    break;
                case "C14b":
                    sectionOut.ZJ_PieceWeight = 16.7;
                    break;
                case "C16b":
                    sectionOut.ZJ_PieceWeight = 19.8;
                    break;
                case "C18b":
                    sectionOut.ZJ_PieceWeight = 23;
                    break;
                case "C20b":
                    sectionOut.ZJ_PieceWeight = 25.8;
                    break;
                case "C22b":
                    sectionOut.ZJ_PieceWeight = 28.5;
                    break;
                case "C24b":
                    sectionOut.ZJ_PieceWeight = 30.6;
                    break;
                case "C25b":
                    sectionOut.ZJ_PieceWeight = 31.3;
                    break;
                case "I12.6":
                    sectionOut.ZJ_PieceWeight = 14.2;
                    break;
                case "I14":
                    sectionOut.ZJ_PieceWeight = 16.9;
                    break;
                case "I16":
                    sectionOut.ZJ_PieceWeight = 20.5;
                    break;
                case "I18":
                    sectionOut.ZJ_PieceWeight = 24.1;
                    break;
                case "I20b":
                    sectionOut.ZJ_PieceWeight = 31.1;
                    break;
                case "I22b":
                    sectionOut.ZJ_PieceWeight = 36.5;
                    break;
                case "I24b":
                    sectionOut.ZJ_PieceWeight = 41.2;
                    break;
            }

            switch (zhihu_type)
            {
                case "素混凝土":
                    break;
                case "钢筋砼":
                    Cal_Gangjingtong(sectionIn, sectionOut);
                    break;
                case "喷锚网":
                    Cal_Penmaowang(sectionIn, sectionOut);
                    break;
                case "钢拱架":
                    Cal_gongjia(sectionIn, sectionOut);
                    break;
                case "拱架管棚":
                    Cal_gongjia(sectionIn, sectionOut);
                    Cal_guanpeng(sectionIn, sectionOut);
                    break;
            }

            return sectionOut;
        }
        private static void CalSection(TunnelSectionIn sectionIn, TunnelSectionOut sectionOut)
        {
            //获取输入的参数
            string x = sectionIn.SectionType;//断面类型
            double v = sectionIn.Arch;//拱形
            double B = sectionIn.ClearWidth;//巷道宽度
            double H = sectionIn.ClearHeight;//巷道高度
            double t = sectionIn.CrossSection;//断面支护厚度
            double t1 = sectionIn.BackPlane;//底板支护厚度
            //定义需要计算的参数
            double f = 0; //拱高度
            double zH = 0; //直墙高度
            double r = 0; //小弧参数 r
            double α = 0; //小弧参数 α
            double Ls = 0; //小弧参数 弧长
            double R = 0; //大弧参数R
            double β = 0; //大弧参数β
            double Lb = 0; //大弧参数 弧长
            double S1 = 0; //净断面面积
            double S2 = 0; //掘进断面面积
            double C = 0; //掘进断面周长
            double Hw = 0; //支护体外缘拱高度

            f = B / v;
            zH = H - f;

            switch (x)
            {
                case "三心拱":
                    if (v == 3)
                    {
                        r = 0.260957 * B;
                        R = 0.692 * B;
                        α = 56.31;
                        β = 67.38;
                    }
                    else if (v == 4)
                    {
                        r = 0.172746 * B;
                        R = 0.904509 * B;
                        α = 63.435;
                        β = 53.13;
                    }
                    else if (v == 5)
                    {
                        r = 0.128445 * B;
                        R = 1.128887 * B;
                        α = 68.1986111;
                        β = 43.6027778;
                    }
                    Ls = (r + t) * Math.PI * α / 180;
                    Lb = (R + t) * Math.PI * β / 180;
                    S1 = (2 * r * r * Math.PI * α / 360 + R * R * Math.PI * β / 360 - 0.5 * (B - 2 * r) * (R - f) + zH * B) / 1000000;
                    S2 = (2 * Math.Pow(r + t, 2) * Math.PI * α / 360 + Math.Pow(R + t, 2) * Math.PI * β / 360 - 0.5 * (B - 2 * r) * (R - f) + (B + 2 * t) * (zH + t1) + 2 * (t - t1) * t) / 1000000;
                    C = Lb + 2 * (t + zH + Ls);
                    break;

                case "圆弧拱":
                    R = (B * B / 4 + f * f) / 2 / f;
                    α = 2 * Math.Atan(B / 2 / (R - f)) * 180 / Math.PI;
                    β = 2 * Math.Asin((B / 2 + t) / (R + t)) * 180 / Math.PI;
                    Lb = (R + t) * β * Math.PI / 180;
                    Hw = R + t - Math.Pow((Math.Pow(R + t, 2) - Math.Pow(B / 2 + t, 2)), 0.5);
                    S1 = (R * R * Math.PI * α / 360 - B * (R - f) / 2 + B * zH) / 1000000;
                    S2 = (Math.Pow(R + t, 2) * Math.PI * β / 360 - (B / 2 + t) * (R + t - Hw) + (H + t - Hw + t1) * (B + 2 * t) + 2 * t * (t - t1)) / 1000000;
                    C = (H + 2 * t - Hw) * 2 + Lb;
                    break;

                case "半圆拱":
                    R = f;
                    β = 90;
                    Lb = (f + t) * Math.PI;
                    S1 = (f * f * Math.PI / 2 + zH * B) / 1000000;
                    S2 = (Math.Pow(f + t, 2) * Math.PI / 2 + (zH + t1) * (B + 2 * t) + 2 * t * (t - t1)) / 1000000;
                    C = Lb + 2 * (zH + t);
                    break;
            }

            sectionOut.AmountOfCrown = f;
            sectionOut.StraightWallHeight = zH;
            sectionOut.XH_R = r;
            sectionOut.XH_A = α;
            sectionOut.XH_Arc = Ls;
            sectionOut.DH_R = R;
            sectionOut.DH_B = β;
            sectionOut.DH_Arc = Lb;
            sectionOut.NetSection = S1;
            sectionOut.Section = S2;
            sectionOut.SectionPerimeter = C;
            sectionOut.ZHT_Height = Hw;
            sectionOut.ConcreteAmount = S2 - S1;
            sectionOut.ZJ_Length = C;
        }
        private static void Cal_Gangjingtong(TunnelSectionIn sectionIn,TunnelSectionOut sectionOut)
        {
            double Zhu_D = sectionIn.ZJ_Diameter;//主筋直径
            double Zhu_jianju = sectionIn.ZJ_Spacing;//主筋间距
            double singlelength = sectionOut.ZJ_Length;//主筋单根长度
            double Fu_D = sectionIn.FJ_Diameter;//副筋直径
            double Fu_jianjU = sectionIn.FJ_Spacing;//副筋间距
            double FuNumber = Math.Round(singlelength / Fu_jianjU + 0.5, 0);
            sectionOut.FJ_Radical = double.Parse(FuNumber.ToString());
            double CalLength = sectionIn.GaugeLength;//计算长度
            double SingleWeight = (FuNumber * (CalLength + 43 * Fu_D) * Fu_D * Fu_D * Math.PI / 4 + CalLength / Zhu_jianju * (singlelength + 2 * 43 * Zhu_D) * Zhu_D * Zhu_D * Math.PI / 4) * 7.85 / 1000000 * 1.1;
            sectionOut.DCPJ_Weight = SingleWeight;
            if (sectionIn.Monolayer)
            {
                sectionOut.HNTPJ_Weight = SingleWeight;
            }
            else
            {
                sectionOut.HNTPJ_Weight = SingleWeight * 2;
            }
            sectionOut.HNTPJ_Length = CalLength / 1000;
        }
        private static void Cal_Penmaowang(TunnelSectionIn sectionIn, TunnelSectionOut sectionOut)
        {

            double Wang_D = sectionIn.JSW_Diameter;//金属网直径
            double Wang_wangdu = sectionIn.JSW_Wd;//金属网网度
            double bolt_D = sectionIn.MGL_Diameter;//锚杆直径
            double bolt_L = sectionIn.MGL_Length;//锚杆长度
            double tuo_L = sectionIn.TB_Side;//托板边长
            double tuo_h = sectionIn.TB_Pallet;//托板厚度
            double Wang_Area = sectionOut.ZJ_Length / 1000 * 1.15;
            double Wang_weight = Math.Round(1000 / Wang_wangdu + 1.3, 0) * 2 * 1.1 * Wang_D * Wang_D * Math.PI / 4 * 7.85 / 1000000 * sectionOut.ZJ_Length * 1.12;
            double bolt_Number = Math.Round(sectionOut.ZJ_Length / 1000 - 1, 0);
            double tuo_weight = tuo_L * tuo_L * 7.85 / 1000000 * tuo_h;
            double bolt_weight = (tuo_weight + bolt_D * bolt_D * Math.PI / 4 * bolt_L * 7.85 / 1000000 + 0.15) * bolt_Number;
            sectionOut.JSW_Area = Math.Round(Wang_Area, 1);
            sectionOut.JSW_Weight = Math.Round(Wang_weight, 2);
            sectionOut.TB_Weighyt = Math.Round(tuo_weight, 2);
            sectionOut.MGL_Number = Math.Round(bolt_Number, 0);
            sectionOut.MGL_Weight = Math.Round(bolt_weight, 2);
        }
        private static void Cal_gongjia(TunnelSectionIn sectionIn, TunnelSectionOut sectionOut)
        {

            string gongjia_Type = sectionIn.StentType;//支架类型
            double gongjia_jianju = sectionIn.SupportSpacing;//支架间距
            double ban_h = sectionIn.GB_Thickness;//钢板厚度
            double ban_guige = sectionIn.GB_Standard;//钢板规格
            double ban_number = sectionIn.GB_Number;//钢板数量
            double lagan_D = sectionIn.LG_Diameter;//拉杆直径
            double lagan_jianju = sectionIn.LG_Spacing;//拉杆间距
            double ban_weight = ban_guige * ban_guige * 7.85 * ban_h * ban_number / 1000000;//钢板重量
            double lagan_weight = Math.Round(sectionOut.ZJ_Length / lagan_jianju + 0.5, 0) * (gongjia_jianju + 150) * lagan_D * lagan_D * Math.PI / 4 * 7.85 / 1000000;//拉杆重量
            double xinggang_weight = sectionOut.ZJ_PieceWeight;//型钢重量
            double gongjia_weight_all = Math.Round(xinggang_weight * (sectionOut.ZJ_Length + 500) / 1000, 2) + ban_weight + lagan_weight;//钢拱架总重
            sectionOut.GB_Weight = Math.Round(ban_weight, 2);
            sectionOut.LG_Weight = Math.Round(lagan_weight, 2);
            sectionOut.ZJ_TotalWeight = Math.Round(gongjia_weight_all, 2);
        }
        private static void Cal_guanpeng(TunnelSectionIn sectionIn, TunnelSectionOut sectionOut)
        {

            double guanPeng_D = sectionIn.GP_Diameter;//管棚直径
            double guanPeng_bihou = sectionIn.GP_WallThickness;//管棚壁厚
            double guanPeng_L = sectionIn.GP_Length;//管棚长度
            double guanPeng_jianju = sectionIn.GP_Spacing;//管棚间距
            double guanPeng_weight = (guanPeng_D * guanPeng_D - Math.Pow(guanPeng_D - 2 * guanPeng_bihou, 2)) * Math.PI / 4 * guanPeng_L * 7.85 / 1000000;
            double guanPeng_weight_all = 0;
            if (sectionIn.SectionType == "三心拱")
            {
                guanPeng_weight_all = Math.Round(sectionOut.DH_Arc + 2 * sectionOut.XH_R / guanPeng_jianju, 0) * guanPeng_weight;
            }
            else if (sectionIn.SectionType == "圆弧拱")
            {
                guanPeng_weight_all = Math.Round(sectionOut.DH_Arc / guanPeng_jianju, 0) * guanPeng_weight;
            }
            else if (sectionIn.SectionType == "半圆拱")
            {
                guanPeng_weight_all = Math.Round(sectionOut.DH_Arc / guanPeng_jianju - 5, 0) * guanPeng_weight;
            }
            sectionOut.GZ_Weight = Math.Round(guanPeng_weight, 2);
            sectionOut.GP_Weight = Math.Round(guanPeng_weight_all, 2);
        }
    }
}
